package com.media.playerlib.cover;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.kk.taurus.playerbase.event.OnPlayerEventListener;
import com.kk.taurus.playerbase.player.IPlayer;
import com.kk.taurus.playerbase.player.OnTimerUpdateListener;
import com.kk.taurus.playerbase.receiver.BaseCover;
import com.kk.taurus.playerbase.receiver.PlayerStateGetter;
import com.media.playerlib.R;
import com.media.playerlib.model.DataInter;


/**
 * Created by Taurus on 2018/4/15.
 */

public class LoadingCover extends BaseCover implements OnTimerUpdateListener {

    private TextView netspeed;
    private String netSpeeds;

    public LoadingCover(Context context) {
        super(context);
    }

    @Override
    protected void onCoverAttachedToWindow() {
        super.onCoverAttachedToWindow();
        PlayerStateGetter playerStateGetter = getPlayerStateGetter();
        if (playerStateGetter != null && isInPlaybackState(playerStateGetter)) {
            setLoadingState(playerStateGetter.isBuffering());
        }
    }

    private boolean isInPlaybackState(PlayerStateGetter playerStateGetter) {
        int state = playerStateGetter.getState();
        return state != IPlayer.STATE_END
                && state != IPlayer.STATE_ERROR
                && state != IPlayer.STATE_IDLE
                && state != IPlayer.STATE_INITIALIZED
                && state != IPlayer.STATE_STOPPED;
    }

    @Override
    public void onPlayerEvent(int eventCode, Bundle bundle) {
        switch (eventCode) {
            case OnPlayerEventListener.PLAYER_EVENT_ON_BUFFERING_START:
            case OnPlayerEventListener.PLAYER_EVENT_ON_DATA_SOURCE_SET:
            case OnPlayerEventListener.PLAYER_EVENT_ON_PROVIDER_DATA_START:
            case OnPlayerEventListener.PLAYER_EVENT_ON_SEEK_TO:
                setLoadingState(true);
                break;

            case OnPlayerEventListener.PLAYER_EVENT_ON_VIDEO_RENDER_START:
            case OnPlayerEventListener.PLAYER_EVENT_ON_BUFFERING_END:
            case OnPlayerEventListener.PLAYER_EVENT_ON_STOP:
            case OnPlayerEventListener.PLAYER_EVENT_ON_PROVIDER_DATA_ERROR:
            case OnPlayerEventListener.PLAYER_EVENT_ON_SEEK_COMPLETE:
                setLoadingState(false);
                break;
        }
    }

    @Override
    public void onErrorEvent(int eventCode, Bundle bundle) {
        setLoadingState(false);
    }

    @Override
    public void onReceiverEvent(int eventCode, Bundle bundle) {
        if (eventCode == DataInter.Event.KEY_UPDATE_NET_SPEED) {
            netSpeeds = bundle.getString(DataInter.Key.KEY_UPDATE_NET_SPEED_CONTENT);
            netspeed.post(new Runnable() {
                @Override
                public void run() {
                    if (netspeed.isShown()) {
                        netspeed.setText(netSpeeds);
                    }
                }
            });
        }
    }

    private void setLoadingState(final boolean show) {
        if (netspeed.isShown() && show) {
            return;
        }
        netspeed.post(new Runnable() {
            @Override
            public void run() {
                setCoverVisibility(show ? View.VISIBLE : View.GONE);
            }
        });

    }

    @Override
    public View onCreateCoverView(Context context) {
        return View.inflate(context, R.layout.layout_loading_cover, null);
    }

    @Override
    public int getCoverLevel() {
        return levelMedium(25);
    }

    @Override
    public void onReceiverBind() {
        super.onReceiverBind();

        View view = getView();
        netspeed = view.findViewById(R.id.netspeed);
    }

    @Override
    public void onTimerUpdate(int curr, int duration, int bufferPercentage) {
//
    }

}
